package icu.lookyousmileface.nio.basic;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author shenj
 * @title: NioBufferChannelFileWrite
 * @projectName NettyPro
 * @date 2020/11/30 18:05
 */
public class NioBufferChannelFileWrite {

    private static final String msg = "怒发冲冠，凭栏处、潇潇雨歇。抬望眼、仰天长啸，壮怀激烈。三十功名尘与土，八千里路云和月。莫等闲、白了少年头，空悲切。";

    public static void main(String[] args) {

        try {

            FileOutputStream fileOutputStream = new FileOutputStream(new File("src/main/resources/filedata/data1.txt"));

            FileChannel fileOutputStreamChannel = fileOutputStream.getChannel();

            ByteBuffer fileDataBuffer = ByteBuffer.allocate(1024);

            ByteBuffer putData = fileDataBuffer.put(msg.getBytes());

            //反转
            putData.flip();

            fileOutputStreamChannel.write(putData);

            fileOutputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
